#include "stdafx.h"
#include "wf_editpaste.h"

#include "pf_clipboard.h"
#include "pf_modification.h"

/**
 * Constructor.
 *
 * @param undo true for undo and false for redo.
 */
WF_EditPaste::WF_EditPaste( PF_Container& container,
                                        PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Edit Paste",
                           container, graphicView) {}



WF_EditPaste::~WF_EditPaste() {}


QAction* WF_EditPaste::createGUIAction(LX_Define::ActionType /*type*/, QObject* parent) {
	// tr("Paste")
	QAction* action = new QAction(tr("&Paste"), parent);
#if QT_VERSION >= 0x040600
        action->setIcon(QIcon::fromTheme("edit-paste", QIcon(":/actions/editpaste2.png")));
#else
        action->setIcon(QIcon(":/actions/editpaste2.png"));
#endif
        action->setShortcut(QKeySequence::Paste);
        //action->zetStatusTip(tr("Pastes the clipboard contents"));
	
	
    return action;
}

void WF_EditPaste::init(int status) {
    PF_PreviewActionInterface::init(status);
    trigger();
}



void WF_EditPaste::trigger() {
    deletePreview();

    PF_Modification m(*container, graphicView);
    m.paste(PF_PasteAttribute(targetPoint, 1.0, 0.0, false, ""));
    //std::cout << *PF_Clipboard::instance();

	graphicView->redraw(LX_Define::RedrawDrawing); 

    finish();
}


void WF_EditPaste::mouseMoveEvent(PF_MouseEvent* e) {
    switch (getStatus()) {
    case SetTargetPoint:
        targetPoint = snapPoint(e);

        deletePreview();
        preview->addAllFrom(*PF_CLIPBOARD->getGraphic());
        preview->move(targetPoint);

		if (graphic!=NULL) {
			LX_Define::Unit sourceUnit = PF_CLIPBOARD->getGraphic()->getUnit();
			LX_Define::Unit targetUnit = graphic->getUnit();
			double f = PF_Units::convert(1.0, sourceUnit, targetUnit);
        	preview->scale(targetPoint, PF_Vector(f,f));
		}
        drawPreview();
        break;

    default:
        break;
    }
}



void WF_EditPaste::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        init(getStatus()-1);
    }
}



void WF_EditPaste::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }

    targetPoint = e->getCoordinate();
    trigger();
}



void WF_EditPaste::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetTargetPoint:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Set reference point"),
                                            tr("Cancel"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_EditPaste::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_EditPaste::updateToolBar() {
    if (!isFinished()) {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
    } else {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarMain);
    }
}


// EOF
